package com.wwtech.epp.ui.activity.work;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.ui.activity.work.component.DaggerProductManageComponent;
import com.wwtech.epp.ui.activity.work.contract.ProductManageContract;
import com.wwtech.epp.ui.activity.work.module.ProductManageModule;
import com.wwtech.epp.ui.activity.work.presenter.ProductManagePresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: $description
 * @date 2017/09/01 09:59:00
 */

public class ProductManageActivity extends BaseActivity implements ProductManageContract.View {

    @Inject
    ProductManagePresenter mPresenter;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_product_manage);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private ArrayList<String> titles;
    private FragmentPagerAdapter mAdapter;
    @Override
    protected void initData() {
        LinearLayout linearLayout = (LinearLayout) tabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,
                R.drawable.layout_divider_vertical));
        setTitle("项目管理");
        titles = new ArrayList<>();
        titles.add("我的项目");
        titles.add("参与项目");
        titles.add("星标项目");
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return ProjectListFragment.newInstance(titles.get(position));
            }

            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }

        };
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(2);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.new_project_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.new_project:
                Intent intent = new Intent(this, NewProjectActivity.class);
                startActivityForResult(intent, 0);
                overridePendingTransition(R.anim.activity_translate_in, R.anim.activity_translate_out);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0 && resultCode == 0) {
            EventBus.getDefault().post("refresh");
        }
    }

    @Override
    protected void setupActivityComponent() {
        DaggerProductManageComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .productManageModule(new ProductManageModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(ProductManageContract.ProductManageContractPresenter presenter) {
        mPresenter = (ProductManagePresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

}